<template>
  <div>
    <!-- <TopFilter style="margin: 20px;" :list="topFilterList" :cols="3" :collapse="false" @filterChange="changeHandle" @onCollapse="collapseHandle"></TopFilter> -->
    <FilterTable
      ref="table"
      columnsRef="repairQueryStatisticsIncomeStatisticsServiceAdviserAchievementSummary"
      :columns="columns"
      :dataSource="dataSource"
      :isExportExcel="true"
      exportFileName="服务顾问业绩统计汇总.xlsx"
      :isMemoryPagination="true"
      :isSelectColumn="false"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChangeHandle"
      :onSyncTableData="tableAllData"
      height="450px"
    >
      <template slot="controls" slot-scope="props"> </template>
    </FilterTable>
    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 99,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <el-button @click="closeDialog">关 闭</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin';
import moment from 'moment';
import dictCode from '@/utils/dictCode';
import { arrayObjDeWeight } from '@/utils/index'; // 去重
import { getDealerUser } from '@/api/customerRepair/customerComplaint/customerComplaintReception';
import { searchServiceAdvisorSum } from '@/api/repair/queryStatistics/queryAccounts';
import { searchRoleByCode } from '@/api/common/empSelect/index';

export default {
  name: 'LHMServiceAdviserAchievementSummary',
  components: {},
  mixins: [dictionary],
  props: ['formData'],
  data() {
    return {
      topFilterList: this.createTopFilters(),
      dataSource: [],
      columns: this.createTableCheckColumns()
    };
  },
  mounted() {
    // 分页
    this.BaseTable = this.$refs.table.$refs.pageTable;
    this.BaseTable.START_LOADING();
    this.searchServiceAdvisorSum();
    // this.getDealerUser();
    this.getFwgcsSelectFun();
  },
  methods: {
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    // 搜索按钮
    async changeHandle(val) {
      // console.log(val);
      if (!val || val.groupCondition.length < 1) {
        return this.$notify.warn({ title: '提示信息', message: '请选择' });
      }
      let params = { groupCondition: val.groupCondition.join(',') };
      // console.log(params, 123123123123);

      let condition = Object.assign({}, this.formData, params);
      this.BaseTable.START_LOADING();
      try {
        let res = await searchServiceAdvisorSum(condition);
        this.dataSource = [];
        this.dataSource = res.data;
      } catch (error) {
      } finally {
        this.BaseTable.STOP_LOADING();
      }

      // console.log(123, res.data);
    },
    //  查询条件
    createTopFilters() {
      return [
        {
          type: 'MULTIPLE_CHECKBOX',
          label: '汇总',
          fieldName: 'groupCondition',
          initialValue: ['tt.SERVICE_ADVISOR'],
          itemList: [
            { text: '服务顾问', value: 'tt.SERVICE_ADVISOR' },
            { text: '维修类型', value: 'tt.REPAIR_TYPE_CODE' }
          ],
          // change: this.checkBoxHandle
          rules: [
            {
              validator: (rule, value, callback) => {
                // console.log(value);
                if (!value || value.length < 1) {
                  // console.log(value);
                  return callback(new Error('请选择汇总条件'));
                } else {
                  callback();
                }
              },
              trigger: ['blur', 'change']
            }
          ]
        }
      ];
    },
    createTableCheckColumns() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          sorter: true,
          width: 70,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '服务顾问',
          dataIndex: 'SERVICE_ADVISOR',
          sorter: true,
          showOverflowTooltip: true,
          dictItems: [],
          filter: true,
          filterType: 'checkbox',
          filterItems: []
        },
        {
          title: '车次',
          dataIndex: 'counts',
          sorter: true,
          filter: true,
          align: 'center',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '台次',
          dataIndex: 'countss',
          sorter: true,
          filter: true,
          align: 'center',
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        },
        {
          title: '合计金额',
          dataIndex: 'TOTAL_AMOUNT',
          sorter: true,
          filter: true,
          align: 'right',
          precision: 2,
          summation: true,
          showOverflowTooltip: true,
          filterType: 'number'
        }
      ];
    },
    // 获取所有人的信息
    /* async getDealerUser() {
      const res = await getDealerUser();
      let userData = arrayObjDeWeight(res.data, 'userId');
      this.columns.find(x => x.dataIndex == 'SERVICE_ADVISOR').dictItems = this.columns.find(x => x.dataIndex == 'SERVICE_ADVISOR').filterItems = this.columns.find(
        x => x.dataIndex == 'SERVICE_ADVISOR'
      ).editItems = userData.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },*/

    // 获取所有的服务顾问 服务工程师  hyx
    async getFwgcsSelectFun() {
      let testDrive = { role: dictCode.SERVICE_CONSULTANT };
      const res = await searchRoleByCode(testDrive);
      this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').filterItems = this.columns.find(item => item.dataIndex === 'SERVICE_ADVISOR').dictItems = res.data.map(x => ({
        text: x.employeeName,
        value: x.userId + ''
      }));
    },

    async searchServiceAdvisorSum() {
      try {
        let res = await searchServiceAdvisorSum(this.formData);
        if (res.resultCode == 200) {
          console.log('res', res);
          this.dataSource = res.data;
        }
      } catch (error) {
      } finally {
        this.BaseTable.STOP_LOADING();
      }
    },
    tableAllData(val) {},
    selectChangeHandle(val) {
      this.selectedRow = val;
    },
    closeDialog() {
      this.$emit('close', false);
    }
  }
};
</script>
<style lang="less" scoped></style>
